<?php
/**
 * 财富相关接口
 * */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\FinanceServices;
use App\Http\Services\UserServices;

class ApiRongcloudController extends BaseController
{
    protected $service;
    protected $userServices;

    public function __construct()
    {
        $this->service = new FinanceServices();
        $this->userServices = new UserServices();
    }

    /**
     * 融云回调 音视频房间状态同步
     * https://doc.rongcloud.cn/rtcserver/-/-/room/room-status-callback
     */
    public function rongcloudVoiceCallback(Request $request){
        $callback_data = file_get_contents("php://input");
        // write_log('rongcloudcallback', 'rongcloudVoiceCallback INFO', $callback_data);
        try {
            $callback_data = json_decode($callback_data, true);
            if(!empty($callback_data['userId']) && !empty($callback_data['event'])) {
                //用户结束通话
                if(in_array($callback_data['event'], [3, 12, 15, 17, 18, 22])) {
                    $this->userServices->identityStatusUpdate($callback_data['userId'], 1, ['identity_status'=>3]);
                }
                //用户进入通话
                if(in_array($callback_data['event'], [2, 11, 23])) {
                    $this->userServices->identityStatusUpdate($callback_data['userId'], 3, ['identity_status'=>1]);
                }
            }
        } catch (\Exception $e) {
            write_log('rongcloudcallback', "rongcloudVoiceCallback ERROR" .  $e->getMessage(), $callback_data);
            return;
        }

    }
}
